package com.mypack.crm.service.follow.impl;

import com.github.pagehelper.PageHelper;
import com.mypack.crm.constants.EnumDeleteFlag;
import com.mypack.crm.dto.follow.FollowRecordDTO;
import com.mypack.crm.dto.follow.FollowRecordSearchDTO;
import com.mypack.crm.entity.CrmFollowRecord;
import com.mypack.crm.mapper.FollowRecordMapper;
import com.mypack.crm.service.follow.FollowService;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class FollowServiceImpl implements FollowService {

    @Autowired
    private FollowRecordMapper followRecordMapper;
    @Autowired
    private MapperFacade mapperFacade;

    @Override
    public void add(FollowRecordDTO followRecordDTO) {


        CrmFollowRecord crmFollowRecord = mapperFacade.map(followRecordDTO, CrmFollowRecord.class);

        crmFollowRecord.setDeleteFlag(EnumDeleteFlag.NO.getCode());
        crmFollowRecord.setCreateTime(new Date());

        followRecordMapper.insert(crmFollowRecord);

    }

    @Override
    public List<FollowRecordDTO> list(FollowRecordSearchDTO searchDTO, boolean isPage) {

        if (isPage) {
            PageHelper.startPage(searchDTO.getPageNum(), searchDTO.getPageSize());
        }

        return followRecordMapper.list(searchDTO);
    }

    @Override
    public void delete(Integer id) {
        if(id == null){
            return;
        }

        CrmFollowRecord crmFollowRecord = new CrmFollowRecord();
        crmFollowRecord.setId(id);
        crmFollowRecord.setDeleteFlag(EnumDeleteFlag.YES.getCode());

        followRecordMapper.updateById(crmFollowRecord);
    }
}
